<script>
export default {
	onLaunch() {
		var qyjl = {
			"msg": "操作成功",
			"code": 200,
			"data": "操作成功",
			"corpInfo": {
				"corpId": "ww89944e61e3e02d29",
				"corpName": "新疆最西域电子商务有限公司",
				"agentId": "1000015",
				"appId": null,
				"secret": null
			},
			"permissions": [],
			"roles": ["ORDINARY_MEMBER"],
			"user": {
				"searchValue": null,
				"createBy": "超管",
				"createById": null,
				"createTime": "2025-03-03 11:49:19",
				"updateBy": "超管",
				"updateById": null,
				"updateTime": "2025-03-19 16:43:22",
				"remark": null,
				"params": {},
				"userId": "228",
				"weUserId": "ZXY094",
				"deptId": "64",
				"deptName": "二区",
				"dept": {
					"searchValue": null,
					"createBy": null,
					"createById": null,
					"createTime": null,
					"updateBy": null,
					"updateById": null,
					"updateTime": null,
					"remark": null,
					"params": {},
					"deptId": "64",
					"parentId": null,
					"ancestors": null,
					"deptName": null,
					"deptEnName": null,
					"orderNum": null,
					"leader": "ZXY018",
					"phone": null,
					"email": null,
					"status": null,
					"delFlag": null,
					"parentName": null,
					"children": []
				},
				"userName": "高瑞",
				"userDepts": null,
				"position": "区域经理",
				"phoneNumber": "",
				"sex": "0",
				"email": "",
				"bizMail": null,
				"leader": "ZXY018",
				"avatar": "",
				"thumbAvatar": null,
				"telephone": "",
				"nickName": "瑞瑞",
				"extAttr": "{\"attrs\":[]}",
				"weUserStatus": 1,
				"qrCode": null,
				"externalProfile": "{\"wechat_channels\":{\"nickname\":\"最西域特色新疆礼\",\"status\":0},\"external_attr\":[],\"external_corp_name\":\"\"}",
				"externalPosition": null,
				"address": null,
				"openUserid": null,
				"kfStatus": null,
				"dataScope": 5,
				"jobNumber": null,
				"userType": null,
				"isAllocate": null,
				"isUserLeave": null,
				"dimissionTime": null,
				"salt": null,
				"status": "0",
				"delFlag": 0,
				"loginIp": "",
				"loginDate": null,
				"isOpenChat": null,
				"roles": [{
					"searchValue": null,
					"createBy": null,
					"createById": null,
					"createTime": "2021-02-13 20:53:40",
					"updateBy": null,
					"updateById": null,
					"updateTime": null,
					"remark": "普通成员",
					"params": {},
					"roleId": "1",
					"roleName": "企微成员",
					"roleKey": "ORDINARY_MEMBER",
					"roleSort": "2",
					"dataScope": "5",
					"status": "0",
					"delFlag": "0",
					"menuIds": null,
					"deptIds": null,
					"baseRole": null,
					"users": null,
					"oldRoleId": null,
					"admin": false
				}],
				"roleIds": null,
				"roleId": null,
				"postIds": null,
				"companyName": "二区",
				"deptIds": "64",
				"openDaily": null,
				"checkIsRoot": false,
				"account": null,
				"defaultAccount": null,
				"admin": false
			},
			"configParamInfo": {
				"firstRegister": true,
				"weAppParamFill": false,
				"chatParamFill": false,
				"redEnvelopesParamFile": false
			}
		}
		
		uni.setStorageSync("userInfowx", qyjl);//区域经理
		
		// var userInfo=uni.getStorageSync("userInfowx");
		// var tokenInfo=uni.getStorageSync("tokenInfo");
		// var timestamp = Date.now();
		// if(!userInfo||timestamp>=tokenInfo.time){
		// }
		
		this.getCode();
		
	},
	onShow() {
		console.log('App Show');
	},
	onHide() {
		console.log('App Hide');
	},
	methods: {
		getCode() {
			let code = this.$tools.getQueryValue('code'); //是否存在code
			if (!code) {
				const corpId = 'ww89944e61e3e02d29'; // 企业微信的企业ID
				const redirectUri = encodeURIComponent('https://scrm.zuixiyu.com/dhcyh5'); // 授权回调地址
				const state = 'STATE_STATE'; // 自定义参数，用于防止CSRF攻击
				var huancun = uni.getStorageSync('huancun');
				const authUrl = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${corpId}&redirect_uri=${redirectUri}&response_type=code&scope=snsapi_base&state=${state}#wechat_redirect`;
				window.location.replace(authUrl);
			} else {
				var codeStorage = uni.getStorageSync('code');
				if (code != codeStorage) {
					uni.setStorageSync('code', code);
					this.toLogin(code);
				} else {
					window.location.href = 'https://scrm.zuixiyu.com/dhcyh5';
				}
			}  
		},
		// 获取token
		toLogin(code) {
			var data = {
				auth_code: code
			};
			var url = 'https://scrm.zuixiyu.com/api/auth/linkLogin';
			this.$axios
				.axios('get', url, data)
				.then((res) => {
					console.log('=1111111111111=', res);
					if (res.code == 200) {
						const timestamp = Date.now();
						uni.setStorageSync('tokenInfo', { time: timestamp + res.data.expires_in * 60 * 1000, token: res.data.access_token });
						console.log('获取code完成');
						this.getUserInfo();
					} else {
						this.$tools.showToast(res.msg);
					}
				})
				.catch((err) => {
					console.log('错误回调', err);
				});
		},
		getUserInfo() {
			console.log('获取个人信息完成');
			var url = 'https://scrm.zuixiyu.com/api/system/user/getInfo';
			console.log('token', url);
			this.$axios
				.axios('get', url, {})
				.then((res) => {
					uni.getStorageSync('userInfowx', res);
					if (res.code == 200) {
						uni.setStorageSync('userInfowx', res);
					} else {
						this.$tools.showToast(res.msg);
					}
				})
				.catch((err) => {
					console.log('错误回调', err);
				});
		},
		
		
		
		refreToken() {
			var userInfo = uni.getStorageSync('userInfo');
			if (!userInfo) {
				return false;
			}
			var data = {
				refreshToken: uni.getStorageSync('userInfo').data.token.refresh_token
			};
			this.$axios
				.axios('post', this.$paths.getRefreshTokenPost, data)
				.then((res) => {
					console.log('res', res);
					var userInfo = uni.getStorageSync('userInfo');
					if (res.refresh_token) {
						userInfo.data.token.refresh_token = res.refresh_token;
					}
					if (res.access_token) {
						userInfo.data.token.access_token = res.access_token;
					}
					uni.setStorageSync('userInfo', userInfo);
				})
				.catch((err) => {
					console.log('错误回调', err);
				});
		}
		
	}
};
</script>

<style lang="scss">
/*每个页面公共css */
/*每个页面公共css */
@import url('/style/style.css');
@import url('/style/style_rpx.css');
@import '/style/ty_style_scss.scss';

.appbg1 {
	background-color: #ff6b6b;
}

.yinyingtxt {
	text-shadow: 2px 2px 4px rgba(128, 128, 128, 0.75);
}

</style>
